In this article, we will discuss what is default spring boot embedded web server, how to change the embedded server and how to configure the embedded server.
Spring Boot includes support for embedded Tomcat, Jetty, and Undertow servers. Most developers use the appropriate “Starter” to obtain a fully configured instance. By default, spring boot embedded tomcat server, which listens for HTTP requests on port 8080.
Spring Boot supports the following embedded servlet containers:
You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.
We use spring-boot-starter-web starter to create Spring boot web applications or Rest API development. The spring-boot-starter-web stater project internally provides below dependency in its pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
You can see that by default Spring boot web starter includes a dependency on starter tomcat.
Again spring-boot-starter-tomcat starter has the following dependencies.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
The spring-boot-starter-tomcat starter brings in all the dependencies need to run Tomcat as an embedded server.
As we knew that, for servlet stack applications, the spring-boot-starter-web includes Tomcat by including spring-boot-starter-tomcat, but you can use spring-boot-starter-jetty or spring-boot-starter-undertow instead.
When switching to a different HTTP server, you need to exclude the default dependencies in addition to including the one you need. Spring Boot provides separate starters for HTTP servers to help make this process as easy as possible.
Let's look at how to configure Jetty and Undertow web servers.
The below Maven example shows how to exclude Tomcat and include Jetty for Spring MVC:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
The below Maven example shows how to exclude Tomcat and include Undertow for Spring MVC:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behaviour configure the web application type in your application.properties, as shown in the following example:
spring.main.web-application-type=none
In a standalone application, the main HTTP port defaults to 8080 but can be set with server.port in application.properties:
Now the server will start on porthttp://localhost:8081.
Similarly, we can do the same if we’re using an application.yml file:
server:
port: 8081
Both files are loaded automatically by Spring Boot if placed in thesrc/main/resources directory of a Maven application.
Check out this article to know how to change default port and context path in spring boot applications.
We can access the port the server is running on from log output or from theServletWebServerApplicationContext through its WebServer. For example, below test case that uses
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) can also inject the actual port into a field by using the @LocalServerPort annotation, as shown in the following example:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyWebIntegrationTests {
@Autowired
ServletWebServerApplicationContext server;
@LocalServerPort
int port;
// ...
}
Note that @LocalServerPort is a meta-annotation for@Value("${local.server.port}").
HTTP response compression is supported by Jetty, Tomcat, and Undertow. It can be enabled in application.properties, as follows:
server.compression.enabled=true
By default, responses are compressed only if their content type is one of the following:
Self-Signed Certificate (SSL) can be configured declaratively by setting the various server.ssl.* properties, typically in application.properties or application.yml. The following example shows setting SSL properties in application.properties:
server.port=8443
server.ssl.key-store=classpath:keystore.jks
server.ssl.key-store-password=secret
server.ssl.key-password=another-secret
Read more about how to configure SSL in Spring boot applications athttps://www.baeldung.com/spring-boot-https-self-signed-certificate